package com.hbean.service.impl;

import com.hbean.entity.SysUser;
import com.hbean.service.LoginService;
import com.hbean.service.SysUserService;
import com.hbean.utils.HUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-09-30 10:45
 * @ModificationHistory:
 */

@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public SysUser loginIn(String loginName, String password) {

        List<SysUser> sysUsers = sysUserService.getDataForCache();//从缓存获取数据
        sysUsers = sysUsers.stream().filter(sysUser -> loginName.equals(sysUser.getSu05()) && 1 == sysUser.getSu20()).collect(Collectors.toList());

        SysUser user = null;
        if(sysUsers.size() == 0){
            log.info("用户 " + loginName + " 不存在");
        }else{
            sysUsers = sysUsers.stream().filter(sysUser -> loginName.equals(sysUser.getSu05())  && HUtil.md5Encrypt(password).equals(sysUser.getSu06())).collect(Collectors.toList());
            if(sysUsers.size() > 0){
                log.info("登录成功 " + sysUsers.get(0).getSu01() + " " + HUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
                user = sysUsers.get(0);
            }else{
                log.info("用户 " + loginName + " 密码不正确");
            }
        }
        return user;
    }

    @Override
    public boolean loginOff(SysUser sysUser) {
        log.info("退出登录 " + sysUser.getSu01() + " " + HUtil.dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
        return true;
    }
}
